<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>vfork</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_010_362">&nbsp;</a>NAME</h4><blockquote>
vfork - create new process; share virtual memory
</blockquote><h4><a name = "tag_000_010_363">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="unistd.h.html">unistd.h</a>&gt;

pid_t vfork(void);
</code>
</pre>
</blockquote><h4><a name = "tag_000_010_364">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>vfork()</i>
function has the same effect as
<i><a href="fork.html">fork()</a></i>,
except that the behaviour is undefined if the process created by
<i>vfork()</i>
either modifies any data other than a variable of type
<b>pid_t</b>
used to store the return value from
<i>vfork()</i>,
or returns from the function in which
<i>vfork()</i>
was called, or calls any other function before successfully calling
<i><a href="_exit.html">_exit()</a></i>
or one of the
<i>exec</i>
family of functions.
</blockquote><h4><a name = "tag_000_010_365">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>vfork()</i>
returns 0 to the child process and returns the process ID of the child process
to the parent process.  Otherwise, -1 is returned to the parent, no child
process is created, and <i>errno</i> is set to indicate the error.
</blockquote><h4><a name = "tag_000_010_366">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>vfork()</i>
function will fail if:
<dl compact>

<dt>[EAGAIN]<dd>
The system-wide limit on the total number of processes under execution would
be exceeded, or the system-imposed limit on the total number of processes
under execution by a single user would be exceeded.

<dt>[ENOMEM]<dd>
There is insufficient swap space for the new process.

</dl>
</blockquote><h4><a name = "tag_000_010_367">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_010_368">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
On some systems,
<i>vfork()</i>
is the same as
<i><a href="fork.html">fork()</a></i>.
<p>
The
<i>vfork()</i>
function differs from
<i><a href="fork.html">fork()</a></i>
only in that the child process can share code and data with the calling
process (parent process).  This speeds cloning activity significantly at a
risk to the integrity of the parent process if
<i>vfork()</i>
is misused.
<p>
The use of
<i>vfork()</i>
for any purpose except as a prelude to an immediate call to a function from
the
<i>exec</i>
family, or to
<i><a href="_exit.html">_exit()</a></i>,
is not advised.
<p>
The
<i>vfork()</i>
function can be used to create new processes without fully copying the address
space of the old process.  If a forked process is simply going to call
<i>exec</i>,
the data space copied from the parent to the child by
<i><a href="fork.html">fork()</a></i>
is not used.  This is particularly inefficient in a paged environment, making
<i>vfork()</i>
particularly useful.  Depending upon the size of the parent's data space,
<i>vfork()</i>
can give a significant performance improvement over
<i><a href="fork.html">fork()</a></i>.
<p>
The
<i>vfork()</i>
function can normally be used just like
<i><a href="fork.html">fork()</a></i>.
It does not work, however, to return while running in the child's context from
the caller of
<i>vfork()</i>
since the eventual return from
<i>vfork()</i>
would then return to a no longer existent stack frame.  Be careful, also, to
call
<i><a href="_exit.html">_exit()</a></i>
rather than
<i><a href="exit.html">exit()</a></i>
if you cannot
<i>exec</i>,
since
<i><a href="exit.html">exit()</a></i>
flushes and closes standard I/O channels, thereby
damaging the parent process' standard I/O data structures.  (Even with
<i><a href="fork.html">fork()</a></i>,
it is wrong to call
<i><a href="exit.html">exit()</a></i>,
since buffered data would then be flushed twice.)
<p>
If signal handlers are invoked in the child process after
<i>vfork()</i>,
they must follow the same rules as other code in the child process.
</blockquote><h4><a name = "tag_000_010_369">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_010_370">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="exec.html">exec</a></i>,
<i><a href="exit.html">exit()</a></i>,
<i><a href="fork.html">fork()</a></i>,
<i><a href="wait.html">wait()</a></i>,
<i><a href="unistd.h.html">&lt;unistd.h&gt;</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
